package com.woniu.userservice.controller;

import com.woniu.userservice.entity.Positions;
import com.woniu.userservice.service.PositionsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * 张腾飞 3/23 10：32
 * 这是地址表
 * @author dell
 */
@Controller
@RequestMapping("/positions")
public class PositionsController {
    @Resource
    private PositionsService positionsService;

    @GetMapping("selectOne")
    public Positions selectOne(Integer id)
    {
        return positionsService.queryById(id);
    }


    /**
     * 张腾飞
     * @param province 市
     * @param city 区
     * @param area 街道
     * @param detailed 详细地址
     * @param receivername 姓名
     * @param phonenumber 手机号
     * @param postnumber 邮编
     * @param isdefault 0不是默认地址，1是默认地址
     * @param userid 用户id  为隐藏
     * @return新增地址判断 和更改默认地址的为同一个接口
     */
    @PostMapping("/insertMsg")
    @ResponseBody
    public String insertMsg(String province ,String city,String area,String detailed,
                            String receivername,String phonenumber ,Integer postnumber,Integer isdefault,Integer userid){
        String positioninfo = province + city + area + detailed;
        System.out.print("详细地址是：" + positioninfo);
        System.out.print("手动输入的地址是："  + detailed);
        //0不是默认地址，1是默认地址
        if (isdefault == 0) {
            //判断该用户存在默认地址没有
            List<Positions> msgNull = positionsService.findById(userid);
           if (msgNull.size()==0){
               //该用户不存在默认地址，强制将其地址改为默认地址
               isdefault = 1;
               positionsService.insertMsg(positioninfo, receivername, phonenumber, postnumber, isdefault, userid);
               return "提交成功";
           }else {
               //如果存在默认地址则正常插入
               //将原来的默认地址取消掉，设置新的默认地址
               //positionsService.updateByIsdefault();
               positionsService.insertMsgTwo(positioninfo, receivername, phonenumber, postnumber, isdefault, userid);
               return  "更改成功";
           }

        }else if(isdefault ==1){
            positionsService.insertMsgThree(positioninfo, receivername, phonenumber, postnumber, isdefault, userid);
            return "更改成功";
        }else {
            return null;
        }

    }

    /**
     * 查询存储的地址
     * @param uid
     * @return
     */
    @PostMapping("/showTable")
    @ResponseBody
    public List<Positions> positionsTable(@RequestParam(value = "uid",required = false) Integer uid){

        List<Positions> positions =positionsService.findByUid(uid);
        return  positions;
    }
    /**
     * 查询默认地址
     */
    @PostMapping("/showDefaultTable")
    public ModelAndView defaultTable(@RequestParam(value = "uid") Integer uid){
        List<Positions> positions = positionsService.findByOneUid(uid);
        ModelAndView mo = new ModelAndView();
        mo.setViewName("dizhi");
        mo.addObject("po",positions);
        return mo;
    }

    /**
     * 删除地址
     */
    @GetMapping("/deleteAddress")
    public String deleteAddress(@RequestParam(value = "id",required = true) Integer id,Integer userid){

        Boolean bo = positionsService.deleteByuserId(id,userid);
        if (bo){
            return "删除成功";
        }else{
        return "删除失败";
        }
    }
}